package com.cheyunkeji.er.activity.auction;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.app.UserInfoManager;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

public class EditingInformationActivity extends BaseActivity {

    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.tv_name)
    EditText tvName;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.btn_submit_modify)
    Button btn_submit_modify;
    private String sex;//性别

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_editing_information);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        vTopbar.setTitle("个人资料编辑");
        vTopbar.setLeftBack();
        tvAccount.setText(UserInfoManager.getInstance(this).getMobile());
        tvName.setText(UserInfoManager.getInstance(this).getRealName());
        btn_submit_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setdata();
            }
        });

        setSex(UserInfoManager.getInstance(this).getGender());
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //   性别 0-保密 1-男 2-女
                switch (checkedId) {
                    case R.id.baomiBut:
                        sex = "0";
                        break;
                    case R.id.nanBut:
                        sex = "1";
                        break;
                    case R.id.nvBut:
                        sex = "2";
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * |
     * 修改数据
     */
    private void setdata() {
        if (TextUtils.isEmpty(tvName.getText().toString())) {
            SToast.show("姓名不能为空", Gravity.CENTER);
            return;
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("realname", tvName.getText().toString());
        params.put("sex", sex);
        ApiClient.postForm(HttpConstants.Profile, params,

                new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {

                        if (e instanceof UnknownHostException || e instanceof SocketException) {
                            SToast.show(R.string.tip_network_error, Gravity.CENTER);
                            return;
                        }
                    }

                    @Override
                    public void onResponse(String response, int id) {

                        try {
                            JSONObject obj = new JSONObject(response);
                            if (obj.optString("code").equals("1")) {
                                UserInfoManager.getInstance(MyApplication.getInstance())
                                        .saveGender(sex)
                                        .saveRealName(tvName.getText().toString());
                                SToast.show("修改成功");
                            } else {
                                SToast.show(obj.optString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        showDialog();
                    }

                    @Override
                    public void onAfter(int id) {
                        dismissDialog();
                    }

                }
        );

    }


    @Override
    protected void initData() {

    }

    /**
     * 设置性别
     *
     * @param sex
     */
    public void setSex(String sex) {
        this.sex = sex;
        //   性别 0-保密 1-男 2-女
        switch (sex) {
            case "0":
                radioGroup.check(R.id.baomiBut);
                break;
            case "1":
                radioGroup.check(R.id.nanBut);
                break;
            case "2":
                radioGroup.check(R.id.nvBut);
                break;
            default:
                break;
        }
    }
}
